import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);
import mutations from "./mutations";
import actions from "./actions";

//创建store对象

const store = new Vuex.Store({
    state: {
        cartList: []
    },
    mutations: {
        addCart(state,payload) {
            //查找之前是否有该商品
            let oldProduct = state.cartList.find(item => item.iid === payload.iid)

            //判断oldProduct
            if (oldProduct){
                oldProduct.count +=1
                //context.commit('addCounter',oldProduct)
            }else {
                payload.count = 1
                //context.commit('addTocart',oldProduct)
                state.cartList.push(payload);
            }

        },
    },
    actions,
})

//挂载到vue实例
export default store